package com.le.tester.book.effectivejava3rdchinese.articlefortyfive;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;

/**
 * createTime：2022/8/25 10:24
 * description：字母回旋Plus,使用stream
 */
public class AnagramsPlus {

    public static void main(String[] args) throws IOException {

        Path dictionary = Paths.get(args[0]);

        int minGroupSize = Integer.parseInt(args[1]);

        try (Stream<String> words = Files.lines(dictionary)) {
            //大量的使用stream的形式，简短而难以阅读
            words.collect(
                    groupingBy(word -> word.chars().sorted()
                            .collect(StringBuilder::new, (sb, c) -> sb.append((char) c), StringBuilder::append).toString()))
                    .values().stream()
                    .filter(group -> group.size() >= minGroupSize)
                    .map(group -> group.size() + ": " + group)
                    .forEach(System.out::println);
        }
    }
}
